package nsl.com.util.excel.basic;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;

/**
 * Description: create CellStyle，包含表头字体、表内容字体，日期格式和表格边框
 * Created by 4790K_NSL on 2017/4/28.
 */
public class UtilExceStyle {

    static public  void setCellBoder(CellStyle cellStyle){
        cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
        cellStyle.setBorderTop(CellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
        cellStyle.setBorderRight(CellStyle.BORDER_THIN);
    }

    static public Font createHeadFont(Workbook workbook){
        Font headFont =workbook.createFont();
        headFont.setFontHeightInPoints((short) 10); //字体高度
        headFont.setColor(XSSFFont.DEFAULT_FONT_COLOR); //字体颜色
        headFont.setFontName("宋体"); //字体
        headFont.setBoldweight(XSSFFont.BOLDWEIGHT_NORMAL); //宽度
        headFont.setItalic(false); //是否使用斜体
//      font.setStrikeout(true); //是否使用划线
        return headFont;
    }

    static public Font createBodyFont(Workbook workbook){
        Font font =workbook.createFont();
        font.setFontHeightInPoints((short) 10); //字体高度
        font.setColor(XSSFFont.DEFAULT_FONT_COLOR); //字体颜色
        font.setFontName("宋体"); //字体
        font.setBoldweight(XSSFFont.DEFAULT_FONT_SIZE); //宽度
        font.setItalic(false); //是否使用斜体
//      font.setStrikeout(true); //是否使用划线
        return font;
    }

    static public CellStyle createHeadCellStyle(Workbook workbook){
        CellStyle headCellStyle =workbook.createCellStyle();
        Font headFont=createHeadFont(workbook);
        headCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        headCellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
        headCellStyle.setFont(headFont);
        headCellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); //水平布局：居中
        headCellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
        headCellStyle.setWrapText(true);
        setCellBoder(headCellStyle);
        return headCellStyle;
    }

    static public CellStyle createBodyCellStyle(Workbook workbook){
        Font font=createBodyFont(workbook);
        CellStyle cellStyle =workbook.createCellStyle();
        cellStyle.setFont(font);
        cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); //水平布局：居中
        cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
        cellStyle.setWrapText(true);
        setCellBoder(cellStyle);
        return cellStyle;
    }

    static public CellStyle createDateCellStyle(Workbook workbook){
        Font font=createBodyFont(workbook);
        String fomatDateString="yyyy/M/d";
        CellStyle cellStyle=createDateForamtStyle(workbook,fomatDateString);
        cellStyle.setFont(font);
        cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); //水平布局：居中
        cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
        cellStyle.setWrapText(true);
        setCellBoder(cellStyle);
        return cellStyle;
    }

    /**
     * @param workbook
     * @param dateFormat like:"yyyy-MM-dd";"yyyy/M/d"
     * @return
     */
    public static CellStyle createDateForamtStyle(Workbook workbook,String dateFormat) {
        CreationHelper createHelper = workbook.getCreationHelper();
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setDataFormat(createHelper.createDataFormat().getFormat(dateFormat));
        return cellStyle;
    }
}
